package com.imessage.comm;


import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;


@Getter
@Setter
@ToString
public class Query implements Serializable {

    private String  name;

    private Object value;

    private String sql;

    private String table;

    public Query(String name, Object value, String table){
        this.name=name;
        this.value=value;
        this.table=table;
    }

    public Query equalto(){
        if(name.equalsIgnoreCase("id")){
            this.sql=String.format("%s::TEXT =:%s",name,name);
        }else{
            this.sql=String.format("%s =:%s",name,name);
        }
        return this;
    }

    public Query contains(){
        if(name.equalsIgnoreCase("id")){
            this.sql=String.format("%s::TEXT like :%s",name,name);
        }else{
            this.sql=String.format("%s like :%s",name,name);
        }
        this.value="%"+String.format("%s",this.value)+"%";
        return this;
    }

    public Query startsWith(){
        if(name.equalsIgnoreCase("id")){
            this.sql=String.format("%s::TEXT like :%s",name,name);
        }else {
            this.sql=String.format("%s like :%s",name,name);
        }
        this.value=String.format("%s",this.value)+"%";
        return this;
    }

    public Query in(){
        if(name.equalsIgnoreCase("id")){
            this.sql=String.format("%s::TEXT in (:%s)",name,name);
        }else{
            this.sql=String.format("%s in (:%s)",name,name);
        }
        return this;
    }


    public Query ge(String setname){
        if(name.equalsIgnoreCase("id")){
            this.sql=String.format("%s::TEXT >= :%s",name,setname);
        }else {
            this.sql=String.format("%s >= :%s",name,setname);
        }
        name= setname;
        return this;
    }

    public Query le(String setname){
        if(name.equalsIgnoreCase("id")){
            this.sql=String.format("%s::TEXT <= :%s",name,setname);
        }else {
            this.sql=String.format("%s <= :%s",name,setname);
        }
        name= setname;
        return this;
    }

    public Query notin(){
        if(name.equalsIgnoreCase("id")){
            this.sql=String.format("%s::TEXT not in (:%s)",name,name);
        }else{
            this.sql=String.format("%s not in (:%s)",name,name);
        }
        return this;
    }


}
